﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KeelKit.Serialization;

namespace KeelKit.KeelKit_CodeLineCounter
{
    public partial class ctlCLC : UserControl
    {
        public ctlCLC()
        {
            InitializeComponent();
            Common.clc = this;
        }

        public   ClcSetting Setting = null;
        public    void Init()
        {
            string kl = KeelKit.Core.Kit.GetKeelFileName(Common.chDTE);
            if (kl != null)
            {

                KeelExt ked = KeelKit.Core.Kit.ReadKeel(kl);
                if (ked != null)
                {
                    Setting = ked.CodeLineCounter;
                    if (Setting == null)
                    {
                        Setting = new ClcSetting();
                        this.Save();
                    }
                    chkNullLine.Checked = Setting.NoEmptyLine;
                    chkRem.Checked = Setting.NoRemLine;
                    tsbDesinger.Checked = Setting.NoDesingerFile;
                    tsExt.Text = Setting.NoExts;
                }
            }

        }
        public void Save()
        {
            if (Setting == null)
            {
                Init();
            }
            Setting.NoEmptyLine = chkNullLine.Checked;
            Setting.NoRemLine = chkRem.Checked;
            Setting.NoDesingerFile = tsbDesinger.Checked;
            Setting.NoExts = tsExt.Text;
            string kl = KeelKit.Core.Kit.GetKeelFileName(Common.chDTE);
            if (kl != null)
            {

                KeelExt ked = KeelKit.Core.Kit.ReadKeel(kl);
                ked.CodeLineCounter = Setting;
                KeelKit.Core.Kit.WriteKeel(ked, kl);
            }
        }
 
        private void chkNullLine_Click(object sender, EventArgs e)
        {
            if (Setting == null) Init();
            if (Setting != null)
            {
                chkNullLine.Checked = !chkNullLine.Checked;
                Setting.NoEmptyLine = chkNullLine.Checked;
            }
        }

        private void chkRem_Click(object sender, EventArgs e)
        {
            if (Setting == null) Init();
            chkRem.Checked = !chkRem.Checked;
            Setting.NoRemLine = chkRem.Checked;

        }

       
        private void btnCount_Click(object sender, EventArgs e)
        {
            try
            {
                Common.InitEvents();
                Save();
                Counter.DoCalcLines(tvLineCounter,Setting );
            }
            catch (Exception ex )
            {
                Common.ShowInfo("计算代码行数时遇到未知错误:" + ex.Message + ex.StackTrace);
            }
        }

        private void tsbDesinger_Click(object sender, EventArgs e)
        {
            if (Setting == null) Init();
            tsbDesinger.Checked = !tsbDesinger.Checked;
            Setting.NoDesingerFile  = tsbDesinger.Checked;
        }

        private void tsExt_TextChanged(object sender, EventArgs e)
        {
            if (Setting == null) Init();
            Setting.NoExts = tsExt.Text;
        }
 

        private void btnRef_Click(object sender, EventArgs e)
        {
            Init();
        }

       

    }
}
